from datetime import timedelta

from django.db import models
from django.utils import timezone


class PlanEvent(models.Model):
    """计划事件表"""
    event = models.CharField(verbose_name="事件",max_length=128)
    important = models.IntegerField(verbose_name="重要性排序",default=1)
    deduct_point = models.IntegerField(verbose_name="积分扣除标准")
    date = models.DateField(verbose_name="添加日期",default=timezone.now)

    def __str__(self):
        return self.event

    def display_deduct_point(self):
        return self.deduct_point


class EverydayResult(models.Model):
    """每日完成情况"""
    date = models.DateField(verbose_name="日期",default=timezone.now)
    person = models.ForeignKey(verbose_name="人员",to="Person",to_field="id",on_delete=models.CASCADE)
    event = models.ForeignKey(verbose_name="事件",to="PlanEvent",to_field="id",on_delete=models.CASCADE)

    def get_week(self):
        one_week = {1:"周一",2:"周二",3:"周三",4:"周四",5:"周五",6:"周六",7:"周日",}
        return one_week[self.date.isoweekday()]

class Person(models.Model):
    """人员表"""
    person = models.CharField(verbose_name="人员",max_length=64)

    def __str__(self):
        return self.person


class Reward(models.Model):
    """奖励表（按周）"""
    date = models.DateField(verbose_name="起始日期")
    end_date = models.DateField(verbose_name="结束日期",null=True,blank=True)
    person = models.ForeignKey(verbose_name="人员", to="Person", to_field="id", on_delete=models.CASCADE)
    reward = models.IntegerField(verbose_name="奖励金额(元)", default=50)

    def save(self, *args, **kwargs):
        self.end_date = self.date + timedelta(days=6)
        super().save(*args, **kwargs)


class Admin(models.Model):
    """管理员"""
    username = models.CharField(verbose_name="用户名",max_length=32)
    password = models.CharField(verbose_name="密码",max_length=64)

    def __str__(self):
        return self.username
